from django.contrib import admin
from .models import Asset, MaintenanceHistory

class MaintenanceHistoryInline(admin.TabularInline):
    model = MaintenanceHistory
    extra = 0
    readonly_fields = ('created_at',)
    fields = ('maintenance_date', 'description', 'cost', 'performed_by', 'next_maintenance_date')

@admin.register(Asset)
class AssetAdmin(admin.ModelAdmin):
    list_display = ('code', 'name', 'model', 'serial_number', 'asset_type', 'status', 'assigned_to', 'location')
    list_filter = ('asset_type', 'status', 'department')
    search_fields = ('code', 'name', 'model', 'serial_number', 'location')
    readonly_fields = ('created_at', 'updated_at', 'code')
    fieldsets = (
        ('基本信息', {
            'fields': ('code', 'name', 'model', 'serial_number', 'asset_type', 'status')
        }),
        ('设备信息', {
            'fields': ('purchase_date', 'warranty_expiry', 'purchase_price')
        }),
        ('位置信息', {
            'fields': ('location', 'department', 'assigned_to')
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at')
        }),
        ('备注', {
            'fields': ('notes',)
        }),
    )
    inlines = [MaintenanceHistoryInline]

@admin.register(MaintenanceHistory)
class MaintenanceHistoryAdmin(admin.ModelAdmin):
    list_display = ('asset', 'maintenance_date', 'cost', 'performed_by', 'next_maintenance_date')
    list_filter = ('maintenance_date', 'performed_by')
    search_fields = ('asset__name', 'description')
    readonly_fields = ('created_at',)
    date_hierarchy = 'maintenance_date'
